package com.momo.web.controller.sysSetting;

import com.momo.common.common.JSONResult;
import com.momo.common.req.sysSetting.authority.BatchRoleUserReq;
import com.momo.common.req.sysSetting.authority.RoleReq;
import com.momo.common.res.authority.LoginAuthReq;
import com.momo.service.sysSetting.SysRoleService;
import com.momo.web.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;

/**
 * Created by MOMO on 2019/3/22.
 */
@RestController
@RequestMapping(value = "/platform/role", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@Slf4j
public class SysRoleController extends BaseController {
    @Autowired
    private SysRoleService sysRoleService;

    @PostMapping("/add/v1")
    public JSONResult add(@Validated(RoleReq.Add.class) @RequestBody RoleReq roleReq) {
        try {
            return sysRoleService.add(roleReq, redisUser());
        } catch (UnsupportedEncodingException e) {
            log.error("新增角色失败:{}", e.getMessage());
            return JSONResult.errorMap("新增角色失败");
        }
    }

    @PostMapping("/modify/v1")
    public JSONResult modify(@Validated(RoleReq.Modify.class) @RequestBody RoleReq roleReq) {
        try {
            return sysRoleService.modify(roleReq, redisUser());
        } catch (UnsupportedEncodingException e) {
            log.error("编辑角色失败:{}", e.getMessage());
            return JSONResult.errorMap("编辑角色失败");
        }
    }

    @PostMapping("/status/v1")
    public JSONResult status(@Validated(RoleReq.Status.class) @RequestBody RoleReq roleReq) {
        try {
            return sysRoleService.status(roleReq, redisUser());
        } catch (UnsupportedEncodingException e) {
            log.error("编辑角色状态失败:{}", e.getMessage());
            return JSONResult.errorMap("编辑角色状态失败");
        }
    }

    @PostMapping("/rolesToUser/v1")
    public JSONResult rolesToUser(@Validated(BatchRoleUserReq.Status.class) @RequestBody BatchRoleUserReq batchRoleUserReq) {
        try {
            return sysRoleService.rolesToUser(batchRoleUserReq, redisUser());
        } catch (UnsupportedEncodingException e) {
            log.error("为用户授权角色失败:{}", e.getMessage());
            return JSONResult.errorMap("为用户授权角色失败");
        }
    }
    @PostMapping("/aclsToRole/v1")
    public JSONResult aclsToRole(@Validated(BatchRoleUserReq.Permission.class) @RequestBody BatchRoleUserReq batchRoleUserReq) {
        try {
            return sysRoleService.aclsToRole(batchRoleUserReq, redisUser());
        } catch (UnsupportedEncodingException e) {
            log.error("为角色授权权限失败:{}", e.getMessage());
            return JSONResult.errorMap("为角色授权权限失败");
        }
    }

    @PostMapping("/roleHaveAclTree/v1")
    public JSONResult roleHaveAclTree(@Validated(LoginAuthReq.Delete.class) @RequestBody LoginAuthReq loginAuthReq) {
        try {
            return sysRoleService.roleHaveAclTree(loginAuthReq, redisUser());
        } catch (UnsupportedEncodingException e) {
            log.error("查看待授权的角色 权限失败:{}", e.getMessage());
            return JSONResult.errorMap("查看待授权的角色 权限失败");
        }
    }

}
